import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtContentArticleApi } from '#/api/cmt/content/article';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getRangePickerDefaultProps } from '#/utils';
import { getBaseCommunityOptions } from '#/api/cmt/base/community';
import { getMerchantInfoOptions } from '#/api/cmt/base/merchantInfo';
import { getArticleCategoryOptions } from '#/api/cmt/content/articleCategory';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'communityId',
      label: '社区',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择社区',
        api: getBaseCommunityOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择商户',
        api: getMerchantInfoOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'categoryId',
      label: '分类',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择文章分类',
        api: getArticleCategoryOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'title',
      label: '文章标题',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入文章标题',
      },
    },
    {
      fieldName: 'author',
      label: '作者',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入作者',
      },
    },
    {
      fieldName: 'authorUserId',
      label: '作者会员ID',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入作者会员ID',
      },
    },
    {
      fieldName: 'coverImage',
      label: '封面图片',
      component: 'ImageUpload',
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'summary',
      label: '摘要',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 2,
        placeholder: '请输入文章摘要',
      },
    },
    {
      fieldName: 'contentType',
      label: '内容类型',
      component: 'RadioGroup',
      formItemClass: 'col-span-2',
      componentProps: {
        options: [
          { label: '富文本', value: 1 },
          { label: 'Markdown', value: 2 },
        ],
      },
      defaultValue: 1,
    },
    {
      fieldName: 'content',
      label: '文章内容',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 8,
        placeholder: '请输入文章内容',
      },
    },
    {
      fieldName: 'tags',
      label: '标签',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入标签，多个标签用逗号分隔',
      },
    },
    {
      fieldName: 'articleSource',
      label: '文章来源',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入文章来源',
      },
    },
    {
      fieldName: 'sourceUrl',
      label: '来源链接',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入来源链接',
      },
    },
    {
      fieldName: 'isTop',
      label: '是否置顶',
      component: 'RadioGroup',
      formItemClass: 'col-span-1',
      componentProps: {
        options: [
          { label: '否', value: 0 },
          { label: '是', value: 1 },
        ],
      },
      defaultValue: 0,
    },
    {
      fieldName: 'isHot',
      label: '是否热门',
      component: 'RadioGroup',
      formItemClass: 'col-span-1',
      componentProps: {
        options: [
          { label: '否', value: 0 },
          { label: '是', value: 1 },
        ],
      },
      defaultValue: 0,
    },
    {
      fieldName: 'isRecommend',
      label: '是否推荐',
      component: 'RadioGroup',
      formItemClass: 'col-span-1',
      componentProps: {
        options: [
          { label: '否', value: 0 },
          { label: '是', value: 1 },
        ],
      },
      defaultValue: 0,
    },
    {
      fieldName: 'allowComment',
      label: '允许评论',
      component: 'RadioGroup',
      formItemClass: 'col-span-1',
      componentProps: {
        options: [
          { label: '否', value: 0 },
          { label: '是', value: 1 },
        ],
      },
      defaultValue: 1,
    },
    {
      fieldName: 'publishStatus',
      label: '发布状态',
      component: 'RadioGroup',
      formItemClass: 'col-span-2',
      componentProps: {
        options: [
          { label: '草稿', value: 0 },
          { label: '已发布', value: 1 },
          { label: '已下架', value: 2 },
        ],
      },
      defaultValue: 0,
    },
    {
      fieldName: 'sort',
      label: '排序',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入排序',
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'title',
      label: '文章标题',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入文章标题',
      },
    },
    {
      fieldName: 'author',
      label: '作者',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入作者',
      },
    },
    {
      fieldName: 'categoryId',
      label: '分类',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请选择分类',
      },
    },
    {
      fieldName: 'publishStatus',
      label: '发布状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: [
          { label: '草稿', value: 0 },
          { label: '已发布', value: 1 },
          { label: '已下架', value: 2 },
        ],
        placeholder: '请选择发布状态',
      },
    },
    {
      fieldName: 'auditStatus',
      label: '审核状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: [
          { label: '待审核', value: 0 },
          { label: '通过', value: 1 },
          { label: '拒绝', value: 2 },
        ],
        placeholder: '请选择审核状态',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtContentArticleApi.Article>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '文章ID',
      minWidth: 100,
    },
    {
      field: 'title',
      title: '文章标题',
      minWidth: 200,
    },
    {
      field: 'author',
      title: '作者',
      minWidth: 120,
    },
    {
      field: 'coverImage',
      title: '封面图片',
      minWidth: 100,
      cellRender: {
        name: 'CellImage',
        props: { size: 60 },
      },
    },
    {
      field: 'categoryId',
      title: '分类ID',
      minWidth: 100,
    },
    {
      field: 'viewCount',
      title: '浏览次数',
      minWidth: 100,
    },
    {
      field: 'likeCount',
      title: '点赞数',
      minWidth: 100,
    },
    {
      field: 'commentCount',
      title: '评论数',
      minWidth: 100,
    },
    {
      field: 'isTop',
      title: '置顶',
      minWidth: 80,
      slots: { default: 'isTop' },
    },
    {
      field: 'isHot',
      title: '热门',
      minWidth: 80,
      slots: { default: 'isHot' },
    },
    {
      field: 'isRecommend',
      title: '推荐',
      minWidth: 80,
      slots: { default: 'isRecommend' },
    },
    {
      field: 'publishStatus',
      title: '发布状态',
      minWidth: 100,
      slots: { default: 'publishStatus' },
    },
    {
      field: 'auditStatus',
      title: '审核状态',
      minWidth: 100,
      slots: { default: 'auditStatus' },
    },
    {
      field: 'publishTime',
      title: '发布时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
